package cn.edu.nju.software.action.admin;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.UserShow;
import cn.edu.nju.software.bean.storage.User;
import cn.edu.nju.software.service.intf.AdminService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class GetUsersAction extends ActionSupport implements SessionAware {

	private int key;
	private String value;
	private AdminService adminService;
	private Map<String, Object> session;

	public void setKey(int key) {
		this.key = key;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		List<User> users = adminService.searchUsers(key, value);
		Map<Integer, UserShow> usersMap = new LinkedHashMap<Integer, UserShow>(users.size());
		for (User user : users) {
			usersMap.put(user.getUserID(), new UserShow(user));
		}
		users = null;
		session.put(Constants.SESSION_USERS, usersMap);
		return SUCCESS;
	}
	
}
